package com.zut.controller;

import com.alibaba.druid.filter.config.ConfigTools;
import com.github.pagehelper.PageInfo;
import com.zut.domain.Coach;
import com.zut.service.CoachService;
import com.zut.service.StudentService;
import com.zut.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/coach")
public class CoachController {

    @Autowired
    private CoachService coachService;

    @Autowired
    private StudentService studentService;

    /*教练注册*/
    @PostMapping("/register")
    public Result save(@RequestBody Coach coach) throws Exception {
        int status = 0;
        Coach coach1 = coachService.selectById(coach.getId());
        if (coach1 != null) {
            status = 1;
        }
        if (status == 1) {
            return new Result(Code.SAVE_ERR, null, "用户已存在！");
        } else {
            String password = coach.getPassword();
            coach.setPassword(ConfigTools.encrypt(password));
            coachService.save(coach);
            return new Result(Code.SAVE_OK, null, "注册成功！");
        }

    }

    /*教练登录*/
    @PostMapping(value = "/login")
    public Result getUserLogin(@RequestBody Coach coach) throws Exception {
        String password = coach.getPassword();
        coach.setPassword(ConfigTools.encrypt(password));
        Coach coach1 = coachService.getByNP(coach);
        if (coach1 != null) {
            String token = JwtUtil.sign(coach.getId(), coach.getPassword());
            HashMap users = new HashMap();
            users.put("token", token);
            users.put("id", coach.getId());
            users.put("status", coach1.getStatus());
            return new Result(Code.GET_OK, users, "登录成功！");
        } else {
            return new Result(Code.GET_ERR, null, "用户名或密码错误！");
        }
    }

    /*得到所有教练信息*/
    @GetMapping("/{pageNum}/{pageSize}")
    private Result getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Map<String, String>> coachPageInfo = coachService.getAll(pageNum, pageSize);
        Integer code = coachPageInfo.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = coachPageInfo.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, coachPageInfo, msg);
    }

    /*根据id查找教练*/
    @GetMapping("/{id}")
    public Result SelectById(@PathVariable String id) {
        Map<String, String> coach = coachService.getById(id);
        Integer code = coach != null ? Code.GET_OK : Code.GET_ERR;
        String msg = coach != null ? "" : "数据查询失败，请重试！";
        return new Result(code, coach, msg);
    }

    /*根据id删除教练*/
    @DeleteMapping("/{id}")
    public Result DeleteById(@PathVariable String id) {
        boolean flag = coachService.deleteById(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag, flag ? "删除成功" : "删除失败");
    }

    /*修改教练信息*/
    @PutMapping
    public Result Update(@RequestBody Coach coach) throws Exception {
        String password = coach.getPassword();
        if (password != null) {
            coach.setPassword(ConfigTools.encrypt(password));
        }
        boolean flag = coachService.update(coach);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /*根据学员id查询所属教练的训练场地*/
    @GetMapping("place/{id}")
    public Result GetPlace(@PathVariable String id) {
        String coachId = studentService.getCoach(id);
        if (coachId == null) {
            return new Result(Code.GET_ERR, "请先选择您的教练");
        }
        String place = coachService.getPlace(coachId);
        if (place == null) {
            return new Result(Code.GET_ERR, "您的教练未选择训练场地");
        }
        return new Result(Code.GET_OK, place, "查询成功");
    }

    /*返回优秀教练*/
    @GetMapping("/best")
    public Result GetBestCoach() {
        List<Coach> coaches = coachService.getBestCoach();
        Integer code = coaches != null ? Code.GET_OK : Code.GET_ERR;
        String msg = coaches != null ? "" : "数据查询失败，请重试！";
        return new Result(code, coaches, msg);
    }

    /*根据教练id得到平均评价分*/
    @GetMapping("/grade/{id}")
    public Result getAvgGradeById(@PathVariable String id) {
        Map<String, String> coaches = coachService.getAvgGradeById(id);
        Integer code = coaches != null ? Code.GET_OK : Code.GET_ERR;
        String msg = coaches != null ? "" : "数据查询失败，请重试！";
        return new Result(code, coaches, msg);
    }


/*    更换密码,json传输数据
    {
        "id":"123",
            "oldPassword":"123",
            "newPassword":"0987"
    }*/
    @PutMapping("/update")
    public Result updatePwd(@RequestBody HashMap<String, String> map) throws Exception {
        String id = map.get("id");
        String oldPassword = map.get("oldPassword");
        String newPassword = map.get("newPassword");
        String password = coachService.getPassword(id);
        String dePwd = ConfigTools.decrypt(password);
        String enPwd = ConfigTools.encrypt(newPassword);
        if (dePwd.equals(oldPassword)) {
            map.replace("newPassword",enPwd);
            boolean flag = coachService.updatePassword(map);
            return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag, "密码修改成功");
        } else {
            return new Result(Code.UPDATE_ERR, "密码输入错误");

        }
    }
}